home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / irit / function.irt < prev    next >
Encoding:
Text File  |  1993-12-30  |  3.0 KB  |  140 lines

  1. #
  2. # Some routines to demonstrate the function/procedure capability.
  3. #
  4.  
  5. #
  6. # sqr definition. Define twice to make sure nothing bad happens.
  7. #
  8. sqr = function(x):
  9.     return = x * x;
  10. sqr = function(x):
  11.     return = x * x;
  12.  
  13. a = sqr(2);
  14. a;
  15.  
  16. a = sqr(2) + sqr(2);
  17. a;
  18.  
  19. t = 3.14;
  20. sqrt(sqr(t * 3.14));
  21.  
  22. #
  23. # No parameters function. Alias to varlist().
  24. #
  25. vl = procedure():
  26.     varlist();
  27. vl();
  28.  
  29. #
  30. # No parameters function, with local variables. Note varlist acknowledges
  31. # the existance of x & y with in a procedure or a function.
  32. #
  33. vl = procedure():x:y:
  34.     x = 5:
  35.     y = 6:
  36.     varlist();
  37. vl();
  38.  
  39. #
  40. # factorial recursive (what else) definition. Note in order for the function
  41. # to be recognized within itself, we have to define it twice, first time as
  42. # a dummy function and second time, the real recursive definition.
  43. #
  44. factor = function(x):return = x;
  45. factor = function(x):
  46.     if (x <= 1, return = 1, return = x * factor(x - 1));
  47.  
  48. factor(3);
  49. factor(10);
  50. factor(69);
  51.  
  52. #
  53. # Interact with axes.
  54. #
  55. aint = procedure(o):
  56.     interact(list(o, axes));
  57.  
  58. b = box(vector(-3,-2,-1),6,4,2);
  59. aint(b);
  60. aint(cone(vector(0,0,-1),vector(0,0,4),2));
  61.  
  62. #
  63. # Few parameters. Apply operator to two given operand. Note since operators
  64. # are overloaded, we can apply this function to many types.
  65. #
  66. apply = function(oprnd1, oprtr, oprnd2):
  67.     if (oprtr == "+", return = oprnd1 + oprnd2):
  68.     if (oprtr == "*", return = oprnd1 * oprnd2):
  69.     if (oprtr == "-", return = oprnd1 - oprnd2):
  70.     if (oprtr == "/", return = oprnd1 / oprnd2);
  71. apply(5, "*", 6);
  72. apply(vector(1, 2, 3), "+", point(1, 1, 1));
  73. interact(apply(box(vector(-3, -2, -1), 6, 4, 2), "-",
  74.            box(vector(-4, -3, -2), 2, 2, 4)));
  75.  
  76. #
  77. # Having some local variables (can you imagine what this function do?).
  78. #
  79. lcl = function(x):y:z:
  80.     y = x + x:
  81.     z = y + y:
  82.     return = x + y + z;
  83.  
  84. lcl(3);
  85. lcl(5);
  86.  
  87. #
  88. # Call a function within a function.
  89. #
  90. somemath = function(x, y):
  91.     return = sqr(x) * factor(y) + sqr(y) * factor(x);
  92.  
  93. somemath(2, 2);
  94. somemath(4, 3);
  95.  
  96. #
  97. # Computes an approximation to the arclength of a curve, by approximating it
  98. # as a piecewise linear curve with n segments.
  99. #
  100. distptpt = function(pt1, pt2):
  101.     return = sqrt(sqr(coord(pt1, 1) - coord(pt2, 1)) +
  102.           sqr(coord(pt1, 2) - coord(pt2, 2)) +
  103.           sqr(coord(pt1, 3) - coord(pt2, 3)));
  104.  
  105. crvlength = function(crv, n):pd:t:t1:t2:dt:pt1:pt2:i:
  106.     return = 0.0:
  107.     pd = pdomain(crv):
  108.     t1 = nth(pd, 1):
  109.     t2 = nth(pd, 2):
  110.     dt = (t2 - t1) / n:
  111.     pt1 = coerce(ceval(crv, t1), e3):
  112.     for (i = 1, 1, n,
  113.      pt2 = coerce(ceval(crv, t1 + dt * i), e3):
  114.      return = return + distptpt(pt1, pt2):
  115.      pt1 = pt2);
  116.  
  117. crvlength(circle(vector(0.0, 0.0, 0.0), 1.0), 30) / 2;
  118. crvlength(circle(vector(0.0, 0.0, 0.0), 1.0), 100) / 2;
  119. crvlength(circle(vector(0.0, 0.0, 0.0), 1.0), 300) / 2;
  120.  
  121. #
  122. # Set a global variable in a procedure.
  123. #
  124.  
  125. modaxes = procedure():
  126.     axes = vector(1, 2, 3);
  127.  
  128. modaxes();
  129. axes;
  130.  
  131. #
  132. # Make sure operator overloading is still valid inside a function:
  133. #
  134. add = FUNCTION(x, y):
  135.     return = x + y;
  136.  
  137. add(1, 2);
  138. add(vector(1,2,3), point(1,2,3));
  139. add(box(vector(-3, -2, -1), 6, 4, 2), box(vector(-4, -3, -2), 2, 2, 4));
  140.